<?php
class BDPrintCategory extends MVC_SQL {
    private $ACatID=9881;
    public $default_category_id = 0;
    public function __construct() {
        $this->setTable('ams_product_category');
    }

    public function getLeftCategory() {
        $sSQL="SELECT *
				FROM `ams_product_category`
				WHERE `product_category_parent_id` ='". $this->ACatID  ."' AND product_category_status=1 ORDER BY product_category_position ";
        return $this->getRecordsetExtend($sSQL);
    }

    public function getTopCategory() {
        $sSQL="SELECT *
				FROM `ams_product_category`
				WHERE `product_category_parent_id` = {$this->default_category_id} AND product_category_id <> '". $this->ACatID  ."' AND product_category_status=1 ORDER BY product_category_position ";
        return $this->getRecordsetExtend($sSQL);
    }
    public function checkParentID($ID) {
        $sSql = "SELECT count(*) as no FROM ams_product_category WHERE product_category_parent_id='".$ID."'";
        $count = $this->getRecordsetExtend($sSql);
        if($count[0]['no']>0) {
            return true;
        }
        else {
            return false;
        }
    }
    public function doUpdateCategory($aData) {
        if($aData['product_category_yahoo_id']=='') {
            unset($aData['product_category_yahoo_id']);
        }
        return $this->doUpdateByPK($aData);
    }
    public function selectAllBrand() {
        $sSQL="SELECT * FROM `ams_product_category`
					WHERE `product_category_parent_id` IN (
					SELECT `product_category_id` FROM `ams_product_category` WHERE `product_category_parent_id`  IN (
					SELECT `product_category_id` FROM `ams_product_category` WHERE `product_category_parent_id`  ='".$this->ACatID."'))";		
        return $this->getRecordsetExtend($sSQL);
    }
    public function getCategoryByName($name) {
        $sSQL="SELECT * FROM `ams_product_category`
					WHERE `product_category_name` ='".$name."'";
        return $this->getRecordsetExtend($sSQL);
    }
    public function getInfoCategory($iId) {
        return $this->getRecordsetByPK($iId);
    }
    public function getInfoCategoryByYahooID($YID) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_yahoo_id='".$YID."'";
        return $this->getRecordsetByPKExtend($sSql);
    }
    public function check_exit_category_id($id) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_yahoo_id={$id}";
        return $this->getRecordsetExtend($sSql);
    }
    public function checkExistID($ID,$owner_id) {
        $sSql = "SELECT count(*) as no FROM ams_product WHERE product_id='".$ID."' AND owner_id = '{$owner_id}'";
        $count = $this->getRecordsetExtend($sSql);
        if($count[0]['no']>0) {
            return true;
        }
        else {
            return false;
        }
    }
    public function getListCategory($oModel) {
        echo $sSql = "SELECT * FROM ams_product_category WHERE 1 ORDER BY product_category_position ";
        die();
        return $this->getRecordsetExtend($sSql);
    }

    public function getListParentCategory($oModel) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id={$this->default_category_id} ORDER BY product_category_position";

        return NULL;//$this->getRecordsetExtend($sSql);
    }

    public function getListChildCategory($parent_id) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id={$parent_id}  AND product_category_status=1 ORDER BY product_category_position ";
        return $this->getRecordsetExtend($sSql);
    }
    /**
     * CATEGORY CHOOSE
     */
    public function getLastestNumberOfProduct($oModel,$owner_id) {
    //$today=date('Y-m-d');
    //$sSql="SELECT * FROM ams_product WHERE product_date_added = '{$today}'";
        $sSql="SELECT * FROM ams_product AS pro WHERE pro.owner_id = '{$owner_id}'  ORDER BY SUBSTRING( pro.product_id, 1, 1 )  DESC , CAST( SUBSTRING( pro.product_id, 2 ) AS UNSIGNED ) DESC";
        $row = $this->getRecordsetByPKExtend($sSql);
        return $row['product_id'];
    //return $this->getNumRow($sSql);
    }
    public function getListParentCategoryChoose($oModel) {
        $sSql="SELECT * FROM ams_product_category WHERE product_category_parent_id=0 AND product_category_status=1 ORDER BY product_category_position";
        return $this->getRecordsetExtend($sSql);
    }

    public function getListChildCategoryChoose($parent_id) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id={$parent_id} AND product_category_status=1 ORDER BY product_category_position";
        return $this->getRecordsetExtend($sSql);
    }
    public function doInsertGenCategoryId($gen_pro_id,$cat_id,$shop_owner_id) {

        $sSql_product_defaul="SELECT * FROM ams_product_defaul WHERE product_id='1'";
        $sSql_method_defaul="SELECT * FROM ams_other_transport_method_defaul WHERE other_product_id='1'";
        $Rs_product_defaul=$this->getRecordsetExtend($sSql_product_defaul);
        $Rs_method_defaul=$this->getRecordsetExtend($sSql_method_defaul);
        if(is_array($Rs_product_defaul)) {
            $sSql_insert_to_procuct="INSERT INTO ams_product (product_id, product_category_id, product_date_added, ";
            $sSql_insert_to_procuct.="product_store, ";
            $sSql_insert_to_procuct.="product_key_select,product_key1,product_key2,product_key3,product_key4,product_key5,";
            $sSql_insert_to_procuct.="product_set_limit_on_prices,";
            $sSql_insert_to_procuct.="product_limited_assesment_of_the_bidder,";
            $sSql_insert_to_procuct.="product_auto_extend,";
            $sSql_insert_to_procuct.="product_end_soon,";
            $sSql_insert_to_procuct.="product_price_fastener,";
            $sSql_insert_to_procuct.="product_have_to_negotiate_discount,";
            $sSql_insert_to_procuct.="product_auto_open_aution,";
            $sSql_insert_to_procuct.="product_price_wins_minimum,";
            $sSql_insert_to_procuct.="product_auction_notable,";
            $sSql_insert_to_procuct.="product_title_bold_line,";
            $sSql_insert_to_procuct.="product_bakground_color,";
            $sSql_insert_to_procuct.="product_store_hot_auction,";
            $sSql_insert_to_procuct.="product_icon_highlight,";
            $sSql_insert_to_procuct.="product_icon_gift,";
            $sSql_insert_to_procuct.="product_yahoo_point,";
            $sSql_insert_to_procuct.="product_yahoo_point_fees,";
            $sSql_insert_to_procuct.="product_affiliate,";
            $sSql_insert_to_procuct.="product_remuneration_to_partner,";
            $sSql_insert_to_procuct.="product_size_after_packet,";
            $sSql_insert_to_procuct.="product_weight_after_packet,";
            $sSql_insert_to_procuct.="product_use_yahoo,";
            $sSql_insert_to_procuct.="product_set_link_to_price_list_page,";
            $sSql_insert_to_procuct.="product_transport_goods_abroad,owner_id) ";
            $sSql_insert_to_procuct.="values ('".$gen_pro_id."',$cat_id, '".date("Y-m-d")."', ";
            foreach ($Rs_product_defaul as $recordItem) {
                $sSql_insert_to_procuct.="'{$recordItem['product_store']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_key_select']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_key1']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_key2']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_key3']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_key4']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_key5']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_set_limit_on_prices']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_limited_assesment_of_the_bidder']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_auto_extend']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_end_soon']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_price_fastener']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_have_to_negotiate_discount']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_auto_open_aution']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_price_wins_minimum']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_auction_notable']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_title_bold_line']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_bakground_color']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_store_hot_auction']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_icon_highlight']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_icon_gift']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_yahoo_point']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_yahoo_point_fees']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_affiliate']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_remuneration_to_partner']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_size_after_packet']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_weight_after_packet']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_use_yahoo']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_set_link_to_price_list_page']}',";
                $sSql_insert_to_procuct.="'{$recordItem['product_transport_goods_abroad']}',";
                $sSql_insert_to_procuct.="'{$shop_owner_id}')";
                break;
            }
        }else {
            $sSql_insert_to_procuct = "INSERT INTO ams_product (product_id,product_category_id,product_store,product_date_added) values ('".$gen_pro_id."',$cat_id,'1円屋','".date('Y-m-d')."')";
        }
       
        $this->doUpdateExtend($sSql_insert_to_procuct);

        if(is_array($Rs_method_defaul)) {
            $sSql_insert_to_method="INSERT INTO ams_other_transport_method (other_product_id,other_title_of_method,other_link_to_price_list,other_uniform_price) values ";


            foreach ($Rs_method_defaul as $recordItem2) {
                $sSql_insert_to_method.="('";
                $sSql_insert_to_method.=$gen_pro_id;//
                $sSql_insert_to_method.="','{$recordItem2['other_title_of_method']}','{$recordItem2['other_link_to_price_list']}','{$recordItem2['other_uniform_price']}'),";
            }
            $sSql_insert_to_method = substr($sSql_insert_to_method,0,strlen($sSql_insert_to_method)-1);
        }else {
            /*
            $sSql_insert_to_method="INSERT INTO ams_other_transport_method (other_product_id,other_title_of_method,other_uniform_price) values
            ('".$gen_pro_id."','佐川急便　e-コレクト','700')";
            */
        }
        $this->doUpdateExtend($sSql_insert_to_method);

        /**
		
         $sSql="INSERT INTO ams_product (product_id,product_category_id,product_store,product_date_added) values ('".$gen_pro_id."',$cat_id,'1円屋','".date('Y-m-d')."')";
         $this->doUpdateExtend($sSql);
		
         $sSql="INSERT INTO ams_other_transport_method (other_product_id,other_title_of_method,other_uniform_price) values
         ('".$gen_pro_id."','佐川急便　e-コレクト','700')";
         $this->doUpdateExtend($sSql);
		
         */

        return true;
    }

    //
    public function doUpdateAllChildStatus($cat_id,$status) {
        $sSql="UPDATE ams_product_category set product_category_status={$status} WHERE product_category_parent_id = {$cat_id}";
        return $this->doUpdateExtend($sSql);
    }
    public function getAllShopOwner(){
        $sql = "SELECT * FROM ams_admin WHERE admin_status=1 AND admin_role = 3";
        return $this->getRecordsetExtend($sql);
    }

    public function getUserInfo($userID){
        $sql = "SELECT * FROM ams_admin WHERE admin_id='{$userID}' AND admin_role = 3";
        return $this->getRecordsetByPKExtend($sql);
    }

}
